/*

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; version 2
 of the License.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/
#import "MediaItem.h"
#import <Foundation/NSURL.h>
#import "XBMCHttpInterface.h"
#import "IconManager.h"

@implementation MediaItemFactory

-(id)initWithExtensions:(NSArray*)musicExtensions pictureExtensions:(NSArray*)pictureExtensions videoExtensions:(NSArray*)videoExtensions
{
   NSLog(@"MediaItemFactory initWithExtensions");
	self = [super init];
	
	m_musicExtensions = [musicExtensions retain];
	m_pictureExtensions = [pictureExtensions retain];;
	m_videoExtensions = [videoExtensions retain];
	return self;
}

-(MediaItem*)createMediaItem:(NSString*)name path:(NSString*)path isFolder:(BOOL)isFolder
{
   MediaType type;
   NSString* extension = [self getFileExtension: path];
   if (isFolder)
   {
      if ([extension caseInsensitiveCompare: @".m3u"] == NSOrderedSame)
	  {
	     type = kPlaylist;
	  }
	  else
	  {
         type = kFolder;
	  }
   }
   else
   {
      //determine type from extension
	  
	  if ([self isInArray: extension array:m_musicExtensions])
	  {
	     type = kMusic;
	  }
	  else if ([self isInArray: extension array:m_pictureExtensions])
	  {
	     type = kPicture;
	  }
	  else if ([self isInArray: extension array:m_videoExtensions])
	  {
	     type = kVideo;
	  }
	  
	  else
	  {
	     type = kUnknown;
	  }
	  
   }
   return [[[MediaItem alloc] initWithNameAndPath: name path:path  mediaType:type] autorelease];
}

-(NSString*)getFileExtension:(NSString*)path
{
   // search for last "."
   NSRange range = [path rangeOfString:@"." options:NSBackwardsSearch];
   if (range.location == NSNotFound)
   {
      return @"";
   }
   NSString *extension = [path substringFromIndex:range.location];
   return extension;
}

-(BOOL)isInArray:(NSString*) value array:(NSArray*)array
{
   int i=0;
   for(i=0; i<[array count]; ++i)
   {
      NSString* arrayItem = [array objectAtIndex:i];
	  if ([value caseInsensitiveCompare: arrayItem] == NSOrderedSame)
	  {
	     return YES;
	  }
   }
   return NO;
}



- (void)dealloc
{
   [m_musicExtensions release];
   [m_pictureExtensions release];
   [m_videoExtensions release];
   [super dealloc];
}

@end

@implementation MediaItem

-(id)initWithNameAndPath: (NSString*)name path:(NSString*)path mediaType:(MediaType)mediaType
{
   self = [super init];
   m_Name = [name retain];
   m_Path = [path retain];
   m_MediaType = mediaType;
   return self;
}

-(NSString*)getName
{
   return m_Name;
}

-(NSString*)getPath
{
   return m_Path;
}

-(MediaType)getType
{
   return m_MediaType;
}

-(UIImage*)getIcon
{
   switch(m_MediaType)
   {
		case kMusic:
			return [[IconManager sharedInstance] getMusicItemIcon];
		case kPicture:
			return [[IconManager sharedInstance] getPictureItemIcon];
		case kVideo:
			return [[IconManager sharedInstance] getVideoItemIcon];
        case kFolder:
			return [[IconManager sharedInstance] getFolderIcon];
		case kPlaylist:
			return [[IconManager sharedInstance] getPlaylistIcon];
		default:
		    return nil;
   }
}

-(void)dealloc
{
   [m_Name release];
   [m_Path release];
   [super dealloc];
}

@end